package com.longxi.tianjintianbao.ui.presenter.quality;

import android.app.DatePickerDialog;
import android.app.TimePickerDialog;
import android.graphics.Color;
import android.view.View;
import android.widget.DatePicker;
import android.widget.TextView;
import android.widget.TimePicker;

import com.bigkoo.pickerview.builder.TimePickerBuilder;
import com.bigkoo.pickerview.listener.OnTimeSelectListener;
import com.bigkoo.pickerview.view.TimePickerView;
import com.longxi.tianjintianbao.ui.activity.quality.QualityClassListActivity;
import com.longxi.tianjintianbao.ui.base.BaseActivity;
import com.longxi.tianjintianbao.ui.base.BaseDialogFragment;
import com.longxi.tianjintianbao.ui.base.BasePresenter;
import com.longxi.tianjintianbao.ui.view.quality.IQualityStateDfgView;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import me.drakeet.multitype.Items;
import me.drakeet.multitype.MultiTypeAdapter;

public class QualityStateDfgPresenter extends BasePresenter<IQualityStateDfgView> {

    private MultiTypeAdapter adapter;
    private Items items;

    public QualityStateDfgPresenter(BaseActivity context, BaseDialogFragment dialogFragment) {
        super(context, dialogFragment);
    }

    public void setBar() {
        mDialogFragment.setTitle("整改指派");
        mDialogFragment.setTvLeft("返回");
    }

    QualityClassListActivity qualityClassListActivity;
    /**
     * 获取父类
     * @return
     */
    public QualityClassListActivity getQualityClassListActivity(){
        if(qualityClassListActivity == null){
            return (QualityClassListActivity) mContext;
        }else{
            return qualityClassListActivity;
        }
    }

//    public void getCurrentDateTime(boolean haveTime) {
//        //时间选择器
//        TimePickerView pvTime = new TimePickerBuilder(mContext, new OnTimeSelectListener() {
//            @Override
//            public void onTimeSelect(Date date, View v) {
//                getView().getTvLastdate().setText(getTime(date, haveTime));
//            }
//        }).setSubmitColor(Color.RED)//确定按钮文字颜色
//                .setCancelColor(Color.RED)//取消按钮文字颜色
//                .setType(new boolean[]{true, true, true, haveTime, haveTime, haveTime}).build();
//        pvTime.show();
//    }
//
//    private String getTime(Date date, boolean haveTime) {//可根据需要自行截取数据显示
//        SimpleDateFormat format;
//        if(haveTime){
//            format = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
//        }else{
//            format = new SimpleDateFormat("yyyy/MM/dd");
//        }
//
//        return format.format(date);
//    }

    //时间选择哈哈哈
    private int mYear;
    private int mMonth;
    private int mDay;
    private int mHour;
    private int mMinute;
    private TextView mDate;
    private boolean mHaveTime = false;//是否需要时间

    public void getCurrentDateTime(){
        Calendar ca = Calendar.getInstance();
        mYear = ca.get(Calendar.YEAR);
        mMonth = ca.get(Calendar.MONTH);
        mDay = ca.get(Calendar.DAY_OF_MONTH);
        mHour = ca.get(Calendar.HOUR_OF_DAY);
        mMinute = ca.get(Calendar.MINUTE);
        new DatePickerDialog(mContext, onDateSetListener, mYear, mMonth, mDay).show();
    }

    public void getDate(TextView tvEntryDate) {
        mDate = tvEntryDate;
        getCurrentDateTime();
    }

    public void getDate(TextView tvEntryDate, boolean haveTime) {
        mDate = tvEntryDate;
        mHaveTime = haveTime;
        getCurrentDateTime();
    }

    public void getTime(TextView tvEntryDateTime) {
        mDate = tvEntryDateTime;
        new TimePickerDialog(mContext, onTimeSetListener, mHour, mMinute, true).show();
    }

    private TimePickerDialog.OnTimeSetListener onTimeSetListener = new TimePickerDialog.OnTimeSetListener() {
        @Override
        public void onTimeSet(TimePicker timePicker, int hourOfDay, int minute) {
            String times = hourOfDay + ":" + minute;
            mDate.setText(mDate.getText().toString() + " " + times );
        }
    };

    /**
     * 日期选择器对话框监听
     */
    private DatePickerDialog.OnDateSetListener onDateSetListener = new DatePickerDialog.OnDateSetListener() {

        @Override
        public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
            mYear = year;
            mMonth = monthOfYear;
            mDay = dayOfMonth;
            String days;
            if (mMonth + 1 < 10) {
                if (mDay < 10) {
                    days = new StringBuffer().append(mYear).append("/").append("0").
                            append(mMonth + 1).append("/").append("0").append(mDay).append("").toString();
                } else {
                    days = new StringBuffer().append(mYear).append("/").append("0").
                            append(mMonth + 1).append("/").append(mDay).append("").toString();
                }

            } else {
                if (mDay < 10) {
                    days = new StringBuffer().append(mYear).append("/").
                            append(mMonth + 1).append("/").append("0").append(mDay).append("").toString();
                } else {
                    days = new StringBuffer().append(mYear).append("/").
                            append(mMonth + 1).append("/").append(mDay).append("").toString();
                }

            }
            mDate.setText(days);

            if(mHaveTime){
                getTime(mDate);
            }

        }
    };
    //时间选择哈哈哈
}
